<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>计算两个整数的和，输入框输入整数，点击计算按钮计算结果。</p>
    <input type="number" v-model="augend" step="1" />
    +
    <input type="number" v-model="addend" step="1" />
    <button @click="calculate" class="btn-style">计算</button>
    <p>
      两数之和为 <b>{{ result }}</b>
    </p>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return {
      augend: 0,
      addend: 0,
      result: 0,
    };
  },
  methods: {
    calculate() {
      let data = {
        augend: Number(this.augend),
        addend: Number(this.addend),
      };
      console.log(data);
      this.$axios
        .post("/api/add", data)
        .then((res) => {
          console.log(res);
          this.result = res.data.result;
        })
        .catch((err) => {
          console.log("输入的数字类型不对！", err);
        });
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
.btn-style {
  margin-left: 24px;
}
</style>
